home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Jotto ][ 1.2 / source / Jotto code ƒ / jotto dictionary.c next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.0 KB  |  255 lines  |  [TEXT/MMCC]

  1. #include "program globals.h"
  2. #include "file utilities.h"
  3. #include "jotto dictionary.h"
  4. #include "jotto environment.h"
  5. #include "file interface.h"
  6. #include "error.h"
  7. #include "environment.h"
  8. #include "util.h"
  9.  
  10. #define kFiveLetterDict        0
  11. #define kSixLetterDict        1
  12. #define kFiveLetterCustom    2
  13. #define kSixLetterCustom    3
  14.  
  15. short            gComputerFile[2];
  16. short            gHumanFile[4];
  17.  
  18. enum ErrorTypes GetComputerWord(void)
  19. {
  20.     unsigned long    index;
  21.     Boolean            goodword;
  22.     OSErr            isHuman;
  23.     long            count;
  24.     
  25.     goodword=FALSE;
  26.     while (!goodword)
  27.     {
  28.         index=(Random()&0x7fff)%gNumComputerWords[gNumLetters-5];
  29.         SetFPos(gComputerFile[gNumLetters-5], 1, index*(gNumLetters+1));
  30.         count=gNumLetters;
  31.         isHuman=FSRead(gComputerFile[gNumLetters-5], &count, gComputerWord);
  32.  
  33.         goodword=gAllowDup ? TRUE : CheckDup();
  34.     }
  35.     return (isHuman==noErr) ? allsWell :
  36.         (gNumLetters==5) ? kCantGetFiveLetterComputerWord : kCantGetSixLetterComputerWord;
  37. }
  38.  
  39. Boolean CheckDup(void)
  40. {
  41.     short            i,j;
  42.     
  43.     for (i=0; i<gNumLetters; i++)
  44.         for (j=i+1; j<gNumLetters; j++)
  45.             if (gComputerWord[i]==gComputerWord[j])
  46.                 return FALSE;
  47.  
  48.     return TRUE;
  49. }
  50.  
  51. enum ErrorTypes GetHumanWord(char *thisWord, unsigned long index)
  52. {
  53.     long            count;
  54.     OSErr            isHuman;
  55.     
  56.     SetFPos(gHumanFile[gNumLetters-5], 1, index*(gNumLetters+1));
  57.     count=gNumLetters;
  58.     isHuman=FSRead(gHumanFile[gNumLetters-5], &count, thisWord);
  59.     
  60.     return (isHuman==noErr) ? allsWell :
  61.         (gNumLetters==5) ? kCantGetFiveLetterHumanWord : kCantGetSixLetterHumanWord;
  62. }
  63.  
  64. Boolean GetCustomWord(char *thisWord, unsigned long index)
  65. {
  66.     long            count;
  67.     OSErr            isHuman;
  68.     
  69.     if (SetFPos(gHumanFile[gNumLetters-3], 1, (index+1)*(gNumLetters+1))==eofErr)
  70.         return FALSE;
  71.     
  72.     SetFPos(gHumanFile[gNumLetters-3], 1, index*(gNumLetters+1));
  73.     count=gNumLetters;
  74.     isHuman=FSRead(gHumanFile[gNumLetters-3], &count, thisWord);
  75.     
  76.     if (isHuman!=noErr)
  77.         HandleError((gNumLetters==5) ? kCantGetFiveLetterCustomWord :
  78.             kCantGetSixLetterCustomWord, TRUE);
  79.     
  80.     return TRUE;
  81. }
  82.  
  83. enum ErrorTypes SaveCustomWordToDisk(char *thisWord)
  84. {
  85.     long            oldEOF;
  86.     unsigned char    temp[7];
  87.     long            count;
  88.     OSErr            isHuman;
  89.     
  90.     GetEOF(gHumanFile[gNumLetters-3], &oldEOF);
  91.     if (SetEOF(gHumanFile[gNumLetters-3], oldEOF+gNumLetters+1)!=noErr)
  92.     {
  93.         if (gNumLetters==5)
  94.         {
  95.             SetFiveLetterCustomSave(FALSE);
  96.             return kCantSaveFiveLetterCustomWord;
  97.         }
  98.         else
  99.         {
  100.             SetSixLetterCustomSave(FALSE);
  101.             return kCantSaveSixLetterCustomWord;
  102.         }
  103.     }
  104.     SetFPos(gHumanFile[gNumLetters-3], 1, oldEOF);
  105.     Mymemcpy((Ptr)temp, (Ptr)thisWord, 6);
  106.     temp[gNumLetters]=0x0d;
  107.     count=gNumLetters+1;
  108.     isHuman=FSWrite(gHumanFile[gNumLetters-3], &count, temp);
  109.     if (isHuman!=noErr)
  110.     {
  111.         if (gNumLetters==5)
  112.         {
  113.             SetFiveLetterCustomSave(FALSE);
  114.             return kCantSaveFiveLetterCustomWord;
  115.         }
  116.         else
  117.         {
  118.             SetSixLetterCustomSave(FALSE);
  119.             return kCantSaveSixLetterCustomWord;
  120.         }
  121.     }
  122.     
  123.     return allsWell;
  124. }
  125.  
  126. enum ErrorTypes OpenTheFiles(void)
  127. {
  128.     long            thisDirID;
  129.     OSErr            isHuman;
  130.     long            thisEOF;
  131.     FSSpec            fs;
  132.     short            i;
  133.     Boolean            fiveLetterOK;
  134.     Boolean            sixLetterOK;
  135.     Boolean            fiveLetterCustomOK;
  136.     Boolean            sixLetterCustomOK;
  137.     Boolean            fiveLetterCustomSaveOK;
  138.     Boolean            sixLetterCustomSaveOK;
  139.     short            vRefNum;
  140.  
  141.     for (i=0; i<4; i++)
  142.         gComputerFile[i]=gHumanFile[i]=0;
  143.     
  144.     if (GetApplicationParID(&thisDirID)!=noErr)
  145.         return kNoDictionaries;
  146.     
  147.     if (GetApplicationVRefNum(&vRefNum)!=noErr)
  148.         return kNoDictionaries;
  149.     
  150.     FSMakeFSSpec(vRefNum, thisDirID, "\p:Five-letter custom dict", &fs);
  151.     isHuman=FSpCreate(&fs, CREATOR, CUSTOM_TYPE, 0);
  152.     fiveLetterCustomOK=((isHuman==noErr) || (isHuman==dupFNErr));
  153.     
  154.     if (fiveLetterCustomOK)
  155.     {
  156.         isHuman=FSpOpenDF(&fs, fsRdWrPerm, &gHumanFile[kFiveLetterCustom]);
  157.         if (isHuman!=noErr)
  158.             fiveLetterCustomOK=FALSE;
  159.     }
  160.     
  161.     FSMakeFSSpec(vRefNum, thisDirID, "\p:Six-letter custom dict", &fs);
  162.     isHuman=FSpCreate(&fs, CREATOR, CUSTOM_TYPE, 0);
  163.     sixLetterCustomOK=((isHuman==noErr) || (isHuman==dupFNErr));
  164.     
  165.     if (sixLetterCustomOK)
  166.     {
  167.         isHuman=FSpOpenDF(&fs, fsRdWrPerm, &gHumanFile[kSixLetterCustom]);
  168.         if (isHuman!=noErr)
  169.             sixLetterCustomOK=FALSE;
  170.     }
  171.     
  172.     fiveLetterCustomSaveOK=fiveLetterCustomOK;
  173.     sixLetterCustomSaveOK=sixLetterCustomOK;
  174.     
  175.     if (!fiveLetterCustomOK)
  176.     {
  177.         if (!sixLetterCustomOK)
  178.             HandleError(kNoCustomAtAll, FALSE);
  179.         else
  180.             HandleError(kNoFiveLetterCustom, FALSE);
  181.     }
  182.     else
  183.         if (!sixLetterCustomOK)
  184.             HandleError(kNoSixLetterCustom, FALSE);
  185.     
  186.     FSMakeFSSpec(vRefNum, thisDirID, "\p:Five-letter computer dict", &fs);
  187.     isHuman=FSpOpenDF(&fs, fsRdPerm, &gComputerFile[kFiveLetterDict]);
  188.     fiveLetterOK=(isHuman==noErr);
  189.     
  190.     if (fiveLetterOK)
  191.     {
  192.         FSMakeFSSpec(vRefNum, thisDirID, "\p:Five-letter human dict", &fs);
  193.         isHuman=FSpOpenDF(&fs, fsRdPerm, &gHumanFile[kFiveLetterDict]);
  194.         fiveLetterOK=(isHuman==noErr);
  195.     }
  196.     
  197.     if (fiveLetterOK)
  198.     {
  199.         GetEOF(gComputerFile[0], &thisEOF);
  200.         gNumComputerWords[kFiveLetterDict]=thisEOF/6;
  201.         
  202.         GetEOF(gHumanFile[kFiveLetterDict], &thisEOF);
  203.         gNumHumanWords[kFiveLetterDict]=thisEOF/6;
  204.     }
  205.     
  206.     FSMakeFSSpec(vRefNum, thisDirID, "\p:Six-letter computer dict", &fs);
  207.     isHuman=FSpOpenDF(&fs, fsRdPerm, &gComputerFile[kSixLetterDict]);
  208.     sixLetterOK=(isHuman==noErr);
  209.     
  210.     if (sixLetterOK)
  211.     {
  212.         FSMakeFSSpec(vRefNum, thisDirID, "\p:Six-letter human dict", &fs);
  213.         isHuman=FSpOpenDF(&fs, fsRdPerm, &gHumanFile[kSixLetterDict]);
  214.         sixLetterOK=(isHuman==noErr);
  215.     }
  216.     
  217.     if (sixLetterOK)
  218.     {
  219.         GetEOF(gComputerFile[kSixLetterDict], &thisEOF);
  220.         gNumComputerWords[kSixLetterDict]=thisEOF/7;
  221.         
  222.         GetEOF(gHumanFile[kSixLetterDict], &thisEOF);
  223.         gNumHumanWords[kSixLetterDict]=thisEOF/7;
  224.     }
  225.     
  226.     if (!fiveLetterOK)
  227.         gNumLetters=6;
  228.     
  229.     SetDictionaryFlags(fiveLetterOK, sixLetterOK, fiveLetterCustomOK, sixLetterCustomOK,
  230.         fiveLetterCustomSaveOK, sixLetterCustomSaveOK);
  231.     
  232.     return ((fiveLetterOK) || (sixLetterOK)) ? allsWell : kNoDictionaries;
  233. }
  234.  
  235. void CloseTheFiles(void)
  236. {
  237.     if (FiveLetterOKQQ())
  238.     {
  239.         FSClose(gComputerFile[kFiveLetterDict]);
  240.         FSClose(gHumanFile[kFiveLetterDict]);
  241.     }
  242.     
  243.     if (SixLetterOKQQ())
  244.     {
  245.         FSClose(gComputerFile[kSixLetterDict]);
  246.         FSClose(gHumanFile[kSixLetterDict]);
  247.     }
  248.     
  249.     if (FiveLetterCustomOKQQ())
  250.         FSClose(gHumanFile[kFiveLetterCustom]);
  251.     
  252.     if (SixLetterCustomOKQQ())
  253.         FSClose(gHumanFile[kSixLetterCustom]);
  254. }
  255.